﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MotorcycleStore.DomainModel;
using MotorcycleStore.Repository;
using MotorcycleStore.MVC.Administration.ViewModels;

namespace MotorcycleStore.MVC.Areas.Administration.Controllers
{
	public class MotorcycleController : Controller
	{
		IMotorcycleRepository repository;

		public MotorcycleController(IMotorcycleRepository repository)
		{
			this.repository = repository;
		}

		//
		// GET: /Administration/Motorcycle/

		public ActionResult Index()
		{
			return View(repository.Motorcycles);
		}

		//
		// GET: /Administration/Motorcycle/Create

		public ViewResult Create()
		{
			var model = new MotorcycleModel();
			model.AvailableCategories = repository.Categories;
			model.AvailableManufacturers = repository.Manufacturers;

			return View(model);
		}

		//
		// POST: /Administration/Motorcycle/Create
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(MotorcycleModel model)
		{
			if (ModelState.IsValid)
			{
				var motorcycle = new Motorcycle
				{
					Categories = new List<Category>(),
					ManufacturerId = model.Motorcycle.ManufacturerId,
					Name = model.Motorcycle.Name,
					Odometer = model.Motorcycle.Odometer,
					OtherNames = model.Motorcycle.OtherNames,
					Year = model.Motorcycle.Year
				};

				foreach (var categoryId in model.Categories)
				{
					var category = repository.Categories.SingleOrDefault(c => c.Id == categoryId);
					if (category != null)
					{
						motorcycle.Categories.Add(category);
					}
					else
					{
						// TODO: AddModelError
					}
				}

				repository.Add(motorcycle);
				repository.Save();

				return RedirectToAction("Details", new { id = motorcycle.Id });
			}
			model.AvailableManufacturers = repository.Manufacturers;
			model.AvailableCategories = repository.Categories;

			return View(model);
		}

		//
		// GET: /Administration/Motorcycle/Delete/5
		public ViewResult Delete(int id)
		{
			var motorcycle = repository.Motorcycles.SingleOrDefault(m => m.Id == id);
			if (motorcycle == null)
				return View("NotFound");

			return View(motorcycle);
		}

		//
		// POST: /Administration/Motorcycle/Delete/5
		[HttpPost]
		public ActionResult Delete(int id, string confirmDelete)
		{
			var motorcycle = repository.Motorcycles.SingleOrDefault(m => m.Id == id);
			if (motorcycle == null)
				return View("NotFound");

			repository.Delete(id);
			repository.Save();

			TempData["MessageTitle"] = "Motorcycle Deleted";
			TempData["MessageContent"] = String.Format("{0} was deleted", motorcycle.Name);

			return RedirectToAction("Index");
		}

		//
		// GET: /Administration/Motorcycle/Edit/5
		public ViewResult Edit(int id)
		{
			var motorcycle = repository.Motorcycles.SingleOrDefault(m => m.Id == id);
			if (motorcycle == null)
				return View("NotFound");

			var model = new MotorcycleModel
			{
				AvailableCategories = repository.Categories,
				AvailableManufacturers = repository.Manufacturers,
				Motorcycle = motorcycle
			};

			return View(model);
		}

		//
		// GET: /Administration/Motorcycle/Details/5

		public ViewResult Details(int id)
		{
			var motorcycle = repository.Motorcycles.SingleOrDefault(m => m.Id == id);
			if (motorcycle == null)
				return View("NotFound");
			return View(motorcycle);
		}

		// GET: /Administration/Motorcycle/UploadPhoto/5
		public ViewResult UploadPhoto(int id)
		{
			var motorcycle = repository.Motorcycles.SingleOrDefault(m => m.Id == id);
			if (motorcycle == null)
				return View("NotFound");
			return View(motorcycle);
		}

		// POST: /Administration/Motorcycle/UploadPhoto/5
		[HttpPost]
		public ActionResult UploadPhoto(int id, bool? isDefaultPhoto)
		{
			var motorcycle = repository.Motorcycles.SingleOrDefault(m => m.Id == id);
			if (motorcycle == null)
				return View("NotFound");

			// TODO: Validation

			for (int i = 0; i < Request.Files.Count; i++)
			{
				var file = Request.Files[i];
				using (var img = System.Drawing.Image.FromStream(file.InputStream))
				{
					using (var ms = new System.IO.MemoryStream())
					{
						img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

						Photo photo = new Photo
						{
							ContentType = file.ContentType,
							Height = Convert.ToInt16(img.Height),
							Width = Convert.ToInt16(img.Width),
							PhotoData = ms.ToArray()
						};

						if (isDefaultPhoto ?? false)
						{
							motorcycle.MainPhoto = photo;
						}
						else
						{
							motorcycle.Photos.Add(photo);
						}

						repository.Save();
					}
				}
			}

			if (Request.IsAjaxRequest())
			{
				// TODO: return partial
			}

			return RedirectToAction("Details", new { id = id });
		}
	}
}
